package com.ryskoo.permission.managerrolerel.repository;

import com.ryskoo.framework.core.repository.BasicJPAImpl;
import com.ryskoo.permission.managerrolerel.domain.ManagerRoleRelPO;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

@Repository
public class ManagerRoleRelJPAImpl extends BasicJPAImpl<ManagerRoleRelPO> implements IManagerRoleRelRepository {

	/**
	 * 通过管理员id查询角色id
	 */
	@Override
	public List<String> getRoleUuidsByManagerId(String managerId) {
		StringBuilder hql = new StringBuilder("select o.roleId from ManagerRoleRelPO o where 1=1");
		// 如果parentId为空，则只查询一级菜单
		if (!StringUtils.isEmpty(managerId)) {
			hql.append(" and o.managerId =:managerId");
		}
		Query q = this.getH4Session().createQuery(hql.toString());
		if (!StringUtils.isEmpty(managerId)) {
			q.setString("managerId", managerId);
		}
		return q.list();
	}

	/**
	 * 根据管理员id删除其与所有角色的关联关系
	 *
	 * @param managerId
	 *            管理员id
	 */
	@Override
	public void deleteRelByManagerId(String managerId) {
		String hql = "delete from ManagerRoleRelPO where managerId = :managerId";
		Query query = getH4Session().createQuery(hql);
		query.setString("managerId", managerId);
		query.executeUpdate();
	}

}
